package com.web.shop.service.impl;

import com.web.shop.mapper.TbSpecificationMapper;
import com.web.shop.mapper.TbSpecificationOptionMapper;
import com.web.shop.pojo.TbSpecification;
import com.web.shop.pojo.TbSpecificationOption;
import com.web.shop.pojo.TbSpecificationOptionExample;
import com.web.shop.pojogroup.Specification;
import com.web.shop.service.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;



@Service
@Transactional
public class SpecificationServiceImpl implements SpecificationService {

	@Autowired
	private TbSpecificationMapper specificationMapper;

	@Autowired
	private TbSpecificationOptionMapper specificationOptionMapper;

	@Override
	public List<TbSpecification> findAll() {
		return specificationMapper.selectByExample(null);
	}


	@Override
	public void add(Specification specification) {
		// 保存规格  一方的数据
		specificationMapper.insert(specification.getSpecification());
		// 保存规格选项  多方的数据
		for(TbSpecificationOption specificationOption: specification.getSpecificationOptionList()){
			// 设置规格的ID:  主键回填 
			specificationOption.setSpecId(specification.getSpecification().getId());
			
			specificationOptionMapper.insert(specificationOption);
		}
	}


	@Override
	public void update(Specification specification){
		// 修改规格 一方
		specificationMapper.updateByPrimaryKey(specification.getSpecification());
		
		// 先删除规格选项，再添加规格选项
		TbSpecificationOptionExample example = new TbSpecificationOptionExample();
		TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
		
		criteria.andSpecIdEqualTo(specification.getSpecification().getId());
		
		specificationOptionMapper.deleteByExample(example);  //delete from specificatoin_option where spec_id=?
		
		// 保存规格选项
		for(TbSpecificationOption specificationOption: specification.getSpecificationOptionList()){
			// 设置规格的ID:
			specificationOption.setSpecId(specification.getSpecification().getId());
			
			specificationOptionMapper.insert(specificationOption);
		}
	}	

	@Override
	public Specification findOne(Long id){
		Specification specification = new Specification();
		// 根据规格ID查询规格对象
		TbSpecification tbSpecification = specificationMapper.selectByPrimaryKey(id);
		specification.setSpecification(tbSpecification);
		
		// 根据规格的ID查询规格选项
		TbSpecificationOptionExample example = new TbSpecificationOptionExample();
		TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
		criteria.andSpecIdEqualTo(id);
		
		List<TbSpecificationOption> list = specificationOptionMapper.selectByExample(example);
		specification.setSpecificationOptionList(list);
		
		return specification;
	}

	@Override
	public void delete(Long id) {

			// 删除规格
			specificationMapper.deleteByPrimaryKey(id);
			
			// 删除规格选项:
			TbSpecificationOptionExample example = new TbSpecificationOptionExample();
			TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
			criteria.andSpecIdEqualTo(id);
			specificationOptionMapper.deleteByExample(example);

	}
	
}
